//
//  LQUserInfo.m
//  LQProductIntelligenceService
//
//  Created by Yim on 16/6/30.
//  Copyright © 2016年 张宁. All rights reserved.
//

#import "LQUserInfo.h"

static NSString *kLocalCookieName = @"MyProjectCookie";
static NSString *kLocalUserData = @"MyProjectLocalUser";
static NSString *kServerSessionCookie = @"JSESSIONID";

@implementation LQUserInfo
singleton_implementation(LQUserInfo)

/**
 *  获取cookie
 */
- (void) getAndSaveLoginSession
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSArray *allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];
    for (NSHTTPCookie *cookie in allCookies) {
        if ([cookie.name isEqualToString:kServerSessionCookie]) {
            NSMutableDictionary *cookieDic = [NSMutableDictionary dictionaryWithDictionary:[defaults dictionaryForKey:kLocalCookieName]];
            [cookieDic setValue:cookie.properties forKey:@"cookieDict"];
            [defaults setObject:cookieDic forKey:kLocalCookieName];
//            MYLog(@"......%@",[[defaults objectForKey:kLocalCookieName] objectForKey:@"cookieDict"]);
            [defaults synchronize];
        
            break;
        }
    }
}
/**
 *  更新cookie
 */
- (void) updateSession
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSDictionary *cookieDict = [defaults dictionaryForKey:kLocalCookieName];
    NSDictionary *cookieProperties = [cookieDict valueForKey:@"cookieDict"];
    if (cookieProperties != nil) {
        NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
        //        NSArray *cookies = [NSArray arrayWithObject:cookie];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
    }
}
/**
 * 移除cookie
 */
- (void) removeLoginSession
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:kLocalCookieName];
    [defaults synchronize];
}


@end
